<template>
	<view class="userpost" :class="[isDark?'dark':'', AppStyle]" :style="{'background-color':isDark?'#1c1c1c':'#f6f6f6','min-height':isDark?'100vh':'auto'}">
		<view class="header" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar bg-white" :style="{'height': CustomBar + 'px','padding-top':StatusBar + 'px'}">
				<view class="action" @tap="back">
					<text class="cuIcon-back"></text>
				</view>
				<view class="content text-bold" :style="[{top:StatusBar + 'px'}]">
					{{$t('manage.metas.tagclassify')}}
				</view>
				<!--  #ifdef H5 || APP-PLUS -->
				<view class="action" @tap="toAdd">
					<text class="cuIcon-add"></text>
				</view>
				<!--  #endif -->
			</view>
		</view>
		<view :style="[{padding:NavBar + 'px 10px 0px 10px'}]"></view>
		<view class="data-box">
			<view class="cu-bar bg-white search">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input type="text" placeholder="$t('global.gropekey')" v-model="searchText"  @input="searchTag"/>
					<view class="search-close" v-if="searchText!=''" @tap="searchClose()">
						<text class="cuIcon-close"></text>
					</view>
				</view>
			</view>
			<view class="search-type grid col-2">
				<view class="search-type-box" @tap="toType('category')" :class="type=='category'?'active':''">
					<text>{{$t('global.classify')}}</text>
				</view>
				<view class="search-type-box" @tap="toType('tag')" :class="type=='tag'?'active':''">
					<text>{{$t('global.lable')}}</text>
				</view>
			</view>
			<!--  #ifdef MP -->
			<view class="all-btn">
				<view class="user-btn flex flex-direction">
					<button class="cu-btn bg-blue margin-tb-sm lg" @tap="toAdd">{{$t('manage.metas.createclasstag')}}</button>
				</view>
			</view>
			<!--  #endif -->
			<view class="no-data" v-if="metaList.length==0">
				<text class="cuIcon-text"></text>{{$t('common.nodata')}}
			</view>
			<view class="cu-card article no-card">
				<block v-if="type=='category'">
					<view class="cu-item shadow"  v-for="(item,index) in metaList" :key="index">
						<view class="content">
							<view class="desc meta-main">
								<view class="text-content">{{item.name}}
									<text class="meta-id">{{$t('manage.metas.classtagid')}}:{{item.mid}}</text>
								</view>
							</view>
						</view>
						<view class="manage-btn">
							<text class="text-pink radius" @tap="getSubList(item.mid)">{{$t('manage.metas.viewsubord')}}</text>
							<text class="text-green radius" v-if="item.isrecommend==0"  @tap="toRecommend(item.mid,1)">{{$t('global.recommend')}}</text>
							<text class="text-grey radius" v-else  @tap="toRecommend(item.mid,0)">{{$t('global.cancelrecomm')}}</text>
							<text class="text-blue radius" @tap="toEdit(item.mid)">{{$t('common.edit')}}</text>
							<text class="text-red radius" @tap="toDelete(item.mid)">{{$t('common.delete')}}</text>
						</view>
						<view class="category-manage-sub" v-if="item.subList.length>0">
							<view class="cu-item shadow category-item"  v-for="(data,i) in item.subList" :key="i">
								<view class="content">
									<view class="desc meta-main">
										<view class="text-content">{{data.name}}
										<text class="meta-id">{{$t('manage.metas.classtagid')}}:{{data.mid}}</text>
										</view>
									</view>
								</view>
								<view class="manage-btn">
									<text class="text-green radius" v-if="data.isrecommend==0"  @tap="toRecommend(data.mid,1)">{{$t('global.recommend')}}</text>
									<text class="text-grey radius" v-else  @tap="toRecommend(data.mid,0)">{{$t('global.cancelrecomm')}}</text>
									<text class="text-blue radius" @tap="toEdit(data.mid)">{{$t('common.edit')}}</text>
									<text class="text-red radius" @tap="toDelete(data.mid)">{{$t('common.delete')}}</text>
								</view>
							</view>
						</view>
					</view>
				</block>
				<block v-if="type=='tag'">
					<view class="cu-item shadow"  v-for="(item,index) in metaList" :key="index">
						<view class="content">
							<view class="desc meta-main">
								<view class="text-content">{{item.name}}
									<text class="meta-id">{{$t('manage.metas.classtagid')}}:{{item.mid}}</text>
								</view>
							</view>
						</view>
						<view class="manage-btn">
							<text class="cu-btn text-green radius" v-if="item.isrecommend==0"  @tap="toRecommend(item.mid,1)">{{$t('global.recommend')}}</text>
							<text class="cu-btn text-grey radius" v-else  @tap="toRecommend(item.mid,0)">{{$t('global.cancelrecomm')}}</text>
							<text class="cu-btn text-blue radius" @tap="toEdit(item.mid)">{{$t('common.edit')}}</text>
							<text class="cu-btn text-red radius" @tap="toDelete(item.mid)">{{$t('common.delete')}}</text>
						</view>
					</view>
				</block>
				<view class="load-more" @tap="loadMore" v-if="metaList.length>0">
					<text>{{moreText}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { localStorage } from '../../js_sdk/mp-storage/mp-storage/index.js'
	import darkModeMixin from '@/utils/darkModeMixin.js'
	export default {
		mixins: [darkModeMixin],
		data() {
			return {
				StatusBar: this.StatusBar,
				CustomBar: this.CustomBar,
				NavBar:this.StatusBar +  this.CustomBar,
				AppStyle:this.$store.state.AppStyle,
				metaList:[],
				searchText:"",
				type:"category",
				page:1,
				moreText:$t('common.loadmoretxt'),
				
				isLoad:0,
				token:"",
				isLoading:0,
			}
		},
		onPullDownRefresh(){
			var that = this;
			that.page=1;
			that.getMetaList(false);
			var timer = setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000)
			
		},
		onReachBottom() {
		    //触底后执行的方法，比如无限加载之类的
			var that = this;
			if(that.isLoad==0){
				that.loadMore();
			}
		},
		onLoad() {
			var that = this;
			// #ifdef APP-PLUS || MP
			that.NavBar = that.CustomBar;
			// #endif
		},
		onShow(){
			var that = this;
			// #ifdef APP-PLUS
			//plus.navigator.setStatusBarStyle("dark")
			// #endif
			that.getMetaList(false);
			that.page = 1;
			
		},
		methods: {
			loadMore(){
				var that = this;
				that.moreText = that.$t('global.loading');
				that.isLoad=1;
				that.getMetaList(true);
			},
			back(){
				uni.navigateBack({
					delta: 1
				});
			},
			toType(i){
				var that = this;
				that.type=i;
				that.page=1;
				that.metaList = [];
				that.moreText = that.$t('common.loadmoretxt');
				that.isLoad=0;
				that.getMetaList(false);
			},
			getMetaList(isPage){
				var that = this;
				var data = {
					"type":that.type,
				}
				if(that.type=='category'){
					data.parent = 0;
				}
				var page = that.page;
				if(isPage){
					page++;
				}
				that.$Net.request({
					url: that.$API.getMetasList(),
					data:{
						"searchParams":JSON.stringify(that.$API.removeObjectEmptyKey(data)),
						"limit":8,
						"page":page,
						"searchKey":that.searchText,
						"token":that.token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						uni.stopPullDownRefresh()
						that.isLoad=0;
						that.moreText = that.$t('common.loadmoretxt');
						if(res.data.code==1){
							var list = res.data.data;
							for(var i in list){
								list[i].subList = [];
							}
							if(list.length>0){
								if(isPage){
									that.page++;
									that.metaList = that.metaList.concat(list);
								}else{
									that.metaList = list;
								}
							}else{
								that.moreText = that.$t('manage.metas.nomorecontent');
								if(!isPage){
									that.metaList = list;
								}
							}
						}
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					},
					fail: function(res) {
						uni.stopPullDownRefresh()
						that.moreText = that.$t('common.loadmoretxt');
						that.isLoad=0;
						var timer = setTimeout(function() {
							that.isLoading=1;
							clearTimeout('timer')
						}, 300)
					}
				})
			},
			getSubList(mid){
				var that = this;
				var data = {
					"type":that.type,
					"parent":mid
				}
				uni.showLoading({
					title: that.$t('common.loadmore')
				});
				that.$Net.request({
					url: that.$API.getMetasList(),
					data:{
						"searchParams":JSON.stringify(that.$API.removeObjectEmptyKey(data)),
						"limit":50,
						"searchKey":that.searchText,
						"token":that.token
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						uni.hideLoading();
						if(res.data.code==1){
							var list = res.data.data;
							if(list.length>0){
								var metaList = that.metaList;
								for(var i in metaList){
									if(metaList[i].mid == mid){
										metaList[i].subList = list;
									}
								}
							}
						}
					},
					fail: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
					}
				})
			},
			toRecommend(id,type){
				var that = this;
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					'key':id,
					'recommend':type,
					"token":token
				}
				uni.showLoading({
					title: that.$t('common.loadmore')
				});
				that.$Net.request({
					url: that.$API.metaRecommend(),
					data:data,
					header:{
						'Content-Type':'application/x-www-form-urlencoded'
					},
					method: "get",
					dataType: 'json',
					success: function(res) {
						
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
						if(res.data.code==1){
							that.page=1;
							that.moreText = that.$t('common.loadmoretxt');
							that.isLoad=0;
							that.getMetaList(false);
						}
					},
					fail: function(res) {
						setTimeout(function () {
							uni.hideLoading();
						}, 1000);
						uni.showToast({
							title: that.$t('common.networkfail'),
							icon: 'none'
						})
						uni.stopPullDownRefresh()
					}
				})
			},
			toEdit(id){
				uni.navigateTo({
					url: '/pages/manage/metasedit?type=edit&mid='+id
				});
			},
			toDelete(id){
				var that = this;
				var token = "";
				if(localStorage.getItem('userinfo')){
					var userInfo = JSON.parse(localStorage.getItem('userinfo'));
					token=userInfo.token;
				}
				var data = {
					"id":id,
					"token":token
				}
				uni.showModal({
				    title: that.$t('manage.metas.suredeletedata'),
				    success: function (res) {
				        if (res.confirm) {
				            uni.showLoading({
				            	title: that.$t('common.loadmore')
				            });
				            that.$Net.request({
				            	url: that.$API.deleteMeta(),
				            	data:data,
				            	header:{
				            		'Content-Type':'application/x-www-form-urlencoded'
				            	},
				            	method: "get",
				            	dataType: 'json',
				            	success: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: res.data.msg,
				            			icon: 'none'
				            		})
				            		if(res.data.code==1){
				            			that.page=1;
				            			that.moreText = that.$t('common.loadmoretxt');
				            			that.isLoad=0;
				            			that.getMetaList(false);
				            		}
				            	},
				            	fail: function(res) {
				            		setTimeout(function () {
				            			uni.hideLoading();
				            		}, 1000);
				            		uni.showToast({
				            			title: that.$t('common.networkfail'),
				            			icon: 'none'
				            		})
				            	}
				            })
				        } else if (res.cancel) {
				            
				        }
				    }
				});
			},
			toAdd(){
				uni.navigateTo({
					url: '/pages/manage/metasedit?type=add'
				});
			},
			searchTag(){
				var that = this;
				var searchText = that.searchText;
				that.page=1;
				that.getMetaList();
			
			},
			searchClose(){
				var that = this;
				that.searchText = "";
				that.page=1;
				that.getMetaList();
			},
		}
	}
</script>